#!/usr/bin/perl -w
chomp(my @flines = `ls wg*.broadPeak`);
#parse the information about the run out of the broadPeak file.
foreach my $fline (@flines) {
  my $cell_and_feature = '';
  if($fline =~ /wgEncodeBroadChipSeqPeaks(\w+)\.broadPeak/) { $cell_and_feature = $1; }
  else { die; }
  my $cell;
  my $feature;
  if($cell_and_feature =~ /^(\S+)([HCP].*)/) {
    $cell = $1; 
    $feature = $2; 
  }
  print "$cell\t$feature\n";
  `./process_generic_overlay_before_liftover.pl $fline hg18 mm9 $cell $feature`;
#  `cp /tmp/before_liftover.bed /tmp/after_liftover.bed`; # this we don't need to do
  print "Converting to mm9.\nplease save your conversion in the tmp directory as after_liftover.bed and press enter.\n";
  # break into 5000 lines apeice. 
  my $name = 'part';
  open(INF,'/tmp/before_liftover.bed');
  my $index = 1;
  my $appendage = 1;
  while(my $line = <INF>) {
    chomp($line);
    if($index%5000 == 0) {
      $appendage++;
    }
    open(OF,">>/tmp/part$appendage") or die;
    print OF "$line\n";
    close OF;
    $index++;    
  }
  my $wait = <STDIN>;
  `./process_generic_overlay_after_liftover.pl`;
  for(my $i=1; $i<= $appendage; $i++) { unlink("/tmp/part$i"); }
}

#foreach my $fline (@flines) {
#  print "$fline\n";
#}
